<!--
 * @description 组件
 * @author: 陈陈
 * @date: 2025/05/09
 * @phone: 18560000860
 * @email: 18560000860@qq.com
 * @company: 济南晨霜信息技术有限公司 /
 * @business: 承接前后端项目的开发
-->
<template>
	<view
		class="pure-time-picker"
		:class="_classes"
		:style="_styles"
	>
		<pure-picker
			v-bind="{
				..._props,
				dataMode: 'lists',
				cols: cols,
				list: _list,
				indexes: undefined,
				values: _values
			}"
			@onConfirm="_handleConfirm"
			@onCancel="_handleCancel"
			@onClose="_handleClose"
			@onNot="_handleNot"
			@onOk="_handleOk"
			@onChange="_handleChange"
		></pure-picker>
	</view>
</template>

<script>
	import commonComponentOptions from "../../config/options";
	export default {
		options: commonComponentOptions
	};
</script>

<script setup>
	import { computed } from "vue";
	import props from "./props";
	import { isFunction, dateParse, dateData } from "@/uni_modules/pure-utils";

	// 值
	const _values = computed(() => {
		const dateNow = dateParse();
		const dateNowData = dateData(dateNow);
		const yearMonthDayStr = `${dateNowData.year}-${dateNowData.month}-${dateNowData.day}`;
		const dateStr = `${yearMonthDayStr} ${_props?.time?.replace(/时|分|秒/g, ":")}`;
		const date = new Date(dateStr);
		const dd = dateData(date);
		return [dd.hour, dd.minute, dd.second];
	});

	// 小时列表
	const _hours = computed(() => {
		return _getHours();
	});

	// 分钟列表
	const _minutes = computed(() => {
		return _getMinutes();
	});

	// 秒列表
	const _seconds = computed(() => {
		return _getSeconds();
	});

	// 列数据
	const _list = computed(() => [_hours.value, _minutes.value, _seconds.value]);

	// -------------------------------------------------------------------------------- Props
	const _props = defineProps(props);

	// -------------------------------------------------------------------------------- Emits
	const _emits = defineEmits(["onClose", "onConfirm", "onCancel", "onNot", "onOk", "onChange"]);

	// --------------------------------------------------------------------------------
	const _classes = computed(() => {
		const array = [];
		return array;
	});

	// -------------------------------------------------------------------------------- Styles
	const _styles = computed(() => {
		const array = [];
		return array;
	});

	// 获取小时列表
	function _getHours() {
		const hours = [];
		for (let i = 0; i < 24; i++) {
			hours.push({
				label: isFunction(_props.formatter) ? _props.formatter(i, "hour") : `${i}时`,
				value: i,
				full: i < 10 ? `0${i}` : i
			});
		}
		return hours;
	}

	// 获取分钟列表
	function _getMinutes() {
		const minutes = [];
		for (let i = 0; i < 60; i++) {
			minutes.push({
				label: isFunction(_props.formatter) ? _props.formatter(i, "minute") : `${i}分`,
				value: i,
				full: i < 10 ? `0${i}` : i
			});
		}
		return minutes;
	}

	// 获取秒列表
	function _getSeconds() {
		const seconds = [];
		for (let i = 0; i < 60; i++) {
			seconds.push({
				label: isFunction(_props.formatter) ? _props.formatter(i, "second") : `${i}秒`,
				value: i,
				full: i < 10 ? `0${i}` : i
			});
		}
		return seconds;
	}

	// 确认按钮点击事件
	function _handleConfirm(indexes, items) {
		const str = items.map((item) => item.full).join(":");
		_emits("onConfirm", str, items);
	}

	// 取消按钮点击事件
	function _handleCancel() {
		_emits("onCancel");
	}

	// 关闭按钮点击事件
	function _handleClose() {
		_emits("onClose");
	}

	// Header 右侧按钮点击事件
	function _handleOk(indexes, items) {
		const str = items.map((item) => item.full).join(":");
		_emits("onOk", str, items);
	}

	// Header 左侧按钮点击事件
	function _handleNot() {
		_emits("onNot");
	}

	// 选择器改变事件
	function _handleChange(indexes, items) {
		const str = items.map((item) => item.full).join(":");
		_emits("onChange", str, items);
	}
</script>

<style scoped lang="scss">
	@import "./style.scss";
</style>
